#include "dataBase.h"

dataBase::dataBase()
{
    if (!openDatabase()) {
        qCritical() << "Failed to open database";
    }
}

dataBase::~dataBase()
{
    closeDatabase();
}

bool dataBase::openDatabase()
{
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("114.132.169.5");
    db.setDatabaseName("chatServerDataBase");
    db.setUserName("root");
    db.setPassword("lbhxksY12");

    if (!db.open()) {
        qCritical() << "Failed to open database: " << db.lastError().text();
        return false;
    }else{
        qDebug()<<"打开数据库成功！";
    }

    return true;
}

void dataBase::closeDatabase()
{
    if (db.isOpen()) {
        db.close();
    }
}

// userMessage表相关

QStringList dataBase::query_all_userMessage()
{
    QSqlQuery query(db);
    QString sql = "SELECT * FROM userMessage";

    // 准备查询
    query.prepare(sql);
    QStringList result;

    while (query.next()) {
        QSqlRecord record = query.record();
        QString row = record.value("userID").toString() + ","
                      + record.value("userName").toString() + ","
                      + record.value("phoneNumber").toString() + ","
                      + record.value("userPasswd").toString();
        result.append(row);
    }

    return result;
}


QString dataBase::query_userMessage(QString userID)
{
    QSqlQuery query(db);
    QString sql = "SELECT * FROM userMessage where userID = :userID";

    // 准备查询
    query.prepare(sql);
    query.bindValue(":userID", userID);

    if(query.exec() && query.next()){
        QSqlRecord record = query.record();
        QString row = record.value("userID").toString() + ","
                      + record.value("userName").toString() + ","
                      + record.value("phoneNumber").toString() + ","
                      + record.value("userPasswd").toString();
        return row;
    }else{
            return "-1,userName无,PhoneNUmber无,userPasswd无";
    }

}

QString dataBase::query_userPassed(QString userID)
{
    QSqlQuery query(db);
    query.prepare("SELECT userPasswd FROM userMessage WHERE userID = :userID");
    query.bindValue(":userID", userID);

    if (query.exec() && query.next()) {
        return query.value(0).toString();
    }

    return "";
}

void dataBase::modify_userMessage(QString userID, QString username, QString phoneNumber, QString userPasswd)
{
    QSqlQuery query;
    query.prepare("UPDATE userMessage SET userName = :userName, phoneNumber = :phoneNumber, userPasswd = :userPasswd WHERE userID = :userID");
    query.bindValue(":userID", userID);
    query.bindValue(":userName", username);
    query.bindValue(":phoneNumber", phoneNumber);
    query.bindValue(":userPasswd", userPasswd);

    if (!query.exec()) {
        qCritical() << "Failed to update userMessage: " << query.lastError().text();
    }
}

void dataBase::add_userMessage(QString userID, QString username, QString phoneNumber, QString userPasswd)
{
    QSqlQuery query;
    query.prepare("INSERT INTO userMessage (userID, userName, phoneNumber, userPasswd) VALUES (:userID, :userName, :phoneNumber, :userPasswd)");
    query.bindValue(":userID", userID);
    query.bindValue(":userName", username);
    query.bindValue(":phoneNumber", phoneNumber);
    query.bindValue(":userPasswd", userPasswd);

    if (!query.exec()) {
        qCritical() << "Failed to add userMessage: " << query.lastError().text();
    }
}

// userVerify相关

QStringList dataBase::query_all_userVerify()
{
    QSqlQuery query("SELECT * FROM userVerify");
    QStringList result;

    while (query.next()) {
        QSqlRecord record = query.record();
        QString row = record.value("userID").toString() + ","
                      + record.value("userCertificate").toString();
        result.append(row);
    }

    return result;
}

void dataBase::add_userVerify(QString userID, QString userCertificate)
{
    QSqlQuery query;
    query.prepare("INSERT INTO userVerify (userID, userCertificate) VALUES (:userID, :userCertificate)");
    query.bindValue(":userID", userID);
    query.bindValue(":userCertificate", userCertificate);

    if (!query.exec()) {
        qCritical() << "Failed to add userVerify: " << query.lastError().text();
    }
}

// chatMessage相关

void dataBase::add_chatMessage(QString time, QString userID, QString recipient,QString message)
{
    QSqlQuery query;
    query.prepare("INSERT INTO chatMessage (time, userID, recipient, message) VALUES (:time, :userID, :recipient, :message)");
    query.bindValue(":time", time);
    query.bindValue(":userID", userID);
    query.bindValue(":recipient", recipient);
    query.bindValue(":message", message);

    if (!query.exec()) {
        qCritical() << "Failed to add chatMessage: " << query.lastError().text();
    }
}

QStringList dataBase::query_message(QString ID)
{
    QSqlQuery query;
    query.prepare("SELECT * FROM chatMessage WHERE userID = :userID");
    query.bindValue(":userID", ID);

    if (!query.exec()) {
        qCritical() << "Failed to query chatMessage: " << query.lastError().text();
        return QStringList();
    }

    QStringList result;
    while (query.next()) {
        QSqlRecord record = query.record();
        QString row = record.value("time").toString() + ","
                      + record.value("userID").toString() + ","
                      + record.value("recipient").toString() + ","
                      + record.value("message").toString();
        result.append(row);
    }

    return result;
}
